package com.anneric;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;

import com.anneric.PicInfoStorage;

public class Level 
	implements Iterable<PicInfoStorage>{
	
	private ArrayList<PicInfoStorage> LevelLayers;
	private int current_layer_index;
	private int difficulty;
	
	
	public Level(){
		LevelLayers = new ArrayList<PicInfoStorage>();
		difficulty = -1;
		current_layer_index = -1;
	}
	
	public Level(Level levelc){
		LevelLayers = levelc.LevelLayers;
		difficulty = levelc.difficulty;
		current_layer_index = levelc.current_layer_index;
	}
	
	
	//
	// Getter & Setter
	//
	
	public final int getDifficulty(){
		return difficulty;
	}
	
	public final int getAmountOfLayers(){
		return LevelLayers.size();
	}
	
	public final boolean isEmpty(){
		return LevelLayers.isEmpty();
	}
	
	public void addLayer(PicInfoStorage pis){
		checkDiff(pis);
		LevelLayers.add(pis);
	}
	
	private void checkDiff(PicInfoStorage pis) {
		if (pis.hasTwoSpots()){
			difficulty = 2;
		} else {
			difficulty = 1;
		}
	}
	
	public void clear(){
		LevelLayers.clear();
		reset();
	}
	
	public final PicInfoStorage getNextLayer() {
		if (hasNextLayer()){
			++current_layer_index;
			int j = current_layer_index;
			return LevelLayers.get(j);
		} else {
			throw new NoSuchElementException();
		}
	}
	
	public boolean hasNextLayer() {
		if ( !isEmpty() & (current_layer_index < LevelLayers.size()-1) ){
			return true;
		}
		return false;
	}


	public final PicInfoStorage getCurrentLayer() {
		if (current_layer_index > -1){
			return LevelLayers.get(current_layer_index);
		} else {
			throw new NoSuchElementException();
		}
	}
	
	
	public void reset() {
		if (LevelLayers.size() >= 0){
			current_layer_index = 0;
		} else {
			throw new NoSuchElementException("Cannot reset uninstanciated LevelLayer ArrayList.");
		}
	}
	

	
	//
	// Iterator
	//

	public Iterator<PicInfoStorage> iterator() {
		return LevelLayers.iterator();
	}
	
	public Iterator<PicInfoStorage> iterator(int index) {
		return LevelLayers.listIterator(index);
	}
}